
package com.jf.cloud.distribution.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.PrincipalUtil;
import com.jf.cloud.distribution.dto.DistributionAuditingDTO;
import com.jf.cloud.distribution.model.DistributionAuditing;
import com.jf.cloud.distribution.model.DistributionUser;
import com.jf.cloud.distribution.service.DistributionAuditingService;
import com.jf.cloud.distribution.service.DistributionUserService;
import com.jf.cloud.distribution.vo.DistributionAuditingVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;

/**
 * 分销员申请信息
 *
 * @author zz
 * @date 2021-08-09 14:14:05
 */
@RestController("platformDistributionAuditingController")
@RequestMapping("/p/distribution_auditing")
@Tag(name = "platform-分销员申请信息")
public class DistributionAuditingController {

    @Autowired
    private DistributionAuditingService distributionAuditingService;

    @Autowired
    private DistributionUserService distributionUserService;

    @Autowired
    private MapperFacade mapperFacade;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/page")
    @Operation(summary = "获取分销员申请信息列表" , description = "分页获取分销员申请信息列表")
    public ServerResponseEntity<PageVO<DistributionAuditingVO>> page(@Valid PageDTO pageDTO,@ParameterObject DistributionAuditingDTO distributionAuditingDTO) {
        PageVO<DistributionAuditingVO> distributionAuditingPage = distributionAuditingService.pageDistributionAuditing(pageDTO, distributionAuditingDTO);
        setPhoneHide(distributionAuditingPage);
        return ServerResponseEntity.success(distributionAuditingPage);
    }

    @GetMapping
    @Operation(summary = "获取分销员申请信息" , description = "根据auditingId获取分销员申请信息")
    public ServerResponseEntity<DistributionAuditing> getByAuditingId(@RequestParam Long auditingId) {
        return ServerResponseEntity.success(distributionAuditingService.getByAuditingId(auditingId));
    }

    @PutMapping
    @Operation(summary = "审核分销员")
    public ServerResponseEntity<Void> update(@Valid @RequestBody DistributionAuditingDTO distributionAuditingDTO) {
        DistributionAuditing distributionAuditing = mapperFacade.map(distributionAuditingDTO, DistributionAuditing.class);
        distributionAuditing.setUpdateTime(new Date());
        distributionAuditing.setModifier(AuthUserContext.get().getUserId());
        distributionAuditingService.examine(distributionAuditing);
        // 清除缓存
        DistributionUser dbDistributionUser = distributionUserService.getByDistributionUserId(distributionAuditing.getDistributionUserId());
        distributionUserService.removeCacheByUserId(dbDistributionUser.getUserId());
        return ServerResponseEntity.success();
    }

    private void setPhoneHide(PageVO<DistributionAuditingVO> distributionAuditingPage) {
        if (BooleanUtil.isFalse(permission)){
            for (DistributionAuditingVO distributionAuditingVO : distributionAuditingPage.getList()) {
                if (StrUtil.isNotBlank(distributionAuditingVO.getInviteeMobile())) {
                    distributionAuditingVO.setInviteeMobile(PhoneUtil.hideBetween(distributionAuditingVO.getInviteeMobile()).toString());
                }
                if (StrUtil.isNotBlank(distributionAuditingVO.getInvitee()) && PrincipalUtil.isMobile(distributionAuditingVO.getInvitee())) {
                    distributionAuditingVO.setInvitee(PhoneUtil.hideBetween(distributionAuditingVO.getInvitee()).toString());
                }
                if (StrUtil.isNotBlank(distributionAuditingVO.getUserMobile())) {
                    distributionAuditingVO.setUserMobile(PhoneUtil.hideBetween(distributionAuditingVO.getUserMobile()).toString());
                }
                if (StrUtil.isNotBlank(distributionAuditingVO.getNickName()) && PrincipalUtil.isMobile(distributionAuditingVO.getNickName())) {
                    distributionAuditingVO.setNickName(PhoneUtil.hideBetween(distributionAuditingVO.getNickName()).toString());
                }
            }
        }
    }
}
